package com.meteor.web.controller;

import com.meteor.pojo.Comment;
import com.meteor.pojo.DreamComment;
import com.meteor.service.CommentService;
import com.meteor.service.DreamCommentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.rmi.MarshalledObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class WebDreamController {

    @Resource
    private DreamCommentService dreamCommentService;

    @Value("/image/img.png")
    private String avatar;

    @GetMapping("/dream")
    public String dream(Model model) {
        List<DreamComment> comments = dreamCommentService.queryDreamComment();
        model.addAttribute("comments",comments);
        return "dream";
    }


    @PostMapping("/commentOne")
    public String comment(Comment comment, Model model) {
        Date currentTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(currentTime);
        comment.setCreateTime(format);
        comment.setAvatar(avatar);
        DreamComment dc = new DreamComment();
        dc.setId(comment.getId());
        dc.setAvatar(comment.getAvatar());
        dc.setContent(comment.getContent());
        dc.setCreateTime(format);
        dc.setNickname(comment.getNickname());
        dc.setEmail(comment.getEmail());
        dreamCommentService.addDreamComment(dc);
        List<DreamComment> comments = dreamCommentService.queryDreamComment();
        model.addAttribute("comments",comments);
        return "dream::commentList";
    }
}
